package math2;

/**
 * extension of FuntionRToR to represent linear function on domain [a,b] s.t
 * y(a)=y1, y(b)=y2
 * 
 * @author hbui & hannah
 * 
 */
public class RampFunctionRToR extends FunctionRnToR {

	private double alpha, beta;

	/**
	 * constructor of linear function class
	 * 
	 * @param a
	 *            lower part of domain
	 * @param b
	 *            upper part of domain
	 * @param y1
	 *            value of f(a)
	 * @param y2
	 *            value of f(b)
	 */
	public RampFunctionRToR(double a, double b, double y1, double y2) {
		this.alpha = (y2 - y1) / (b - a);
		this.beta = y1 - alpha * a;
	}

	/**
	 * compute the value of linear function at point x
	 * 
	 * @param x
	 * @return value at point x
	 */
	@Override
	public double valueAt(double... x) {
		return alpha * x[0] + beta;
	}

	/**
	 * compute the derivative of linear function at point x
	 * 
	 * @param x
	 * @return derivative at point x
	 */
	@Override
	public double[] gradientAt(double... x) {
		return new double[] { alpha };
	}

	/**
	 * return the order of linear function
	 * 
	 * @return 1
	 */
	@Override
	public int getP() {
		return 1;
	}
}
